package com.itly.controller;

import com.itly.util.ApiResult;
import com.itly.entity.Item;
import com.itly.service.ItemService;
import com.itly.util.ApiResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ItemController {
    private Logger logger = LoggerFactory.getLogger(ItemController.class);

    @Autowired
    private ItemService itemService;

    @PostMapping("item")
    public ApiResult item(@RequestBody Item item) {
        Integer changeNumber = item.getChangeNumber();
        Integer fillNumber = item.getFillNumber();
        Integer judgeNumber = item.getJudgeNumber();
        Integer paperId = item.getPaperId();
        String subject = item.getSubject();
        if (changeNumber == null && fillNumber == null && judgeNumber == null) {
            logger.info("判断选择、填空、判断是否都为空",changeNumber,fillNumber,judgeNumber);
            return ApiResultHandler.buildApiResult(400, "题型数量不能全为空", null);
        } else {
            itemService.cleanPaper(paperId);
            ApiResult apiResult = itemService.randItem(item);
            return apiResult;
        }
    }
}
